/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.view.gui;

import app.view.classes.ViewProfessor;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Murilo
 */
public class CadastrarProfessor extends javax.swing.JFrame {

    private ViewProfessor viewProfessor;

    public CadastrarProfessor() {
        this.viewProfessor = new ViewProfessor();
        initComponents();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        pack();
        setLocation((screenSize.width / 2) - (getWidth() / 2), (screenSize.height / 2) - (getHeight() / 2));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel4 = new javax.swing.JLabel();
        edtNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        edtSobrenome = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        edtDataNascimento = new com.toedter.calendar.JDateChooser();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        edtCodigoMatricula = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        edtDataMatricula = new com.toedter.calendar.JDateChooser();
        btnMatricular = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar - Professor");

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados Pessoais"));

        jLabel4.setText("Nome:");
        jLabel4.setBounds(10, 30, 70, 14);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        edtNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edtNomeActionPerformed(evt);
            }
        });
        edtNome.setBounds(110, 30, 190, 20);
        jLayeredPane1.add(edtNome, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Sobrenome:");
        jLabel3.setBounds(10, 60, 80, 14);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);
        edtSobrenome.setBounds(110, 60, 190, 20);
        jLayeredPane1.add(edtSobrenome, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setText("Data de Nascimento:");
        jLabel1.setBounds(10, 90, 120, 14);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        edtDataNascimento.setBounds(110, 90, 190, 20);
        jLayeredPane1.add(edtDataNascimento, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados do Cadastro"));
        edtCodigoMatricula.setBounds(110, 30, 190, 20);
        jLayeredPane2.add(edtCodigoMatricula, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Data do Cadastro");
        jLabel2.setBounds(10, 60, 100, 14);
        jLayeredPane2.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("Cód do Cadastro");
        jLabel5.setBounds(10, 30, 100, 14);
        jLayeredPane2.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);
        edtDataMatricula.setBounds(110, 60, 190, 20);
        jLayeredPane2.add(edtDataMatricula, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnMatricular.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/7899_64x64.png"))); // NOI18N
        btnMatricular.setText("Cadastrar");
        btnMatricular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMatricularActionPerformed(evt);
            }
        });

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app/view/gui/imgs/sair.png"))); // NOI18N
        btnSair.setText("Cancelar");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
                    .addComponent(jLayeredPane2, javax.swing.GroupLayout.Alignment.LEADING))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnMatricular)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(btnSair)
                .addGap(19, 19, 19))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnMatricular)
                    .addComponent(btnSair))
                .addGap(0, 15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void edtNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edtNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_edtNomeActionPerformed

    private void btnMatricularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMatricularActionPerformed
        if (ValidaCampos()) {
            try {
                this.viewProfessor.cadastrarProfessor(edtNome.getText(), edtSobrenome.getText(),
                        edtDataNascimento.getDate(), edtCodigoMatricula.getText(), edtDataMatricula.getDate(), true);
                JOptionPane.showMessageDialog(this, "Professor Cadastrado com Sucesso.", "Cadastro Professor", JOptionPane.INFORMATION_MESSAGE);
                LimpaCampos();
            } catch (Exception ex) {
                Logger.getLogger(CadastrarProfessor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btnMatricularActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnMatricular;
    private javax.swing.JButton btnSair;
    private javax.swing.JTextField edtCodigoMatricula;
    private com.toedter.calendar.JDateChooser edtDataMatricula;
    private com.toedter.calendar.JDateChooser edtDataNascimento;
    private javax.swing.JTextField edtNome;
    private javax.swing.JTextField edtSobrenome;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    // End of variables declaration//GEN-END:variables

    public boolean ValidaCampos() {
        if (edtNome.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Atenção! Preencha o campo Nome.", "Nome do Professor", JOptionPane.WARNING_MESSAGE);
            return false;
        } else if (edtSobrenome.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Atenção! Preencha o campo Sobrenome", "Sobrenome do Aluno", JOptionPane.WARNING_MESSAGE);
            return false;
        } else if (edtDataNascimento.getDate().toString().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Atenção! Preencha o campo Data de Nascimento.", "Data de Nascimento", JOptionPane.WARNING_MESSAGE);
            return false;
        } else if (edtCodigoMatricula.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Atenção! Preencha o campo Código da Matrícula.", "Código da Matrícula", JOptionPane.WARNING_MESSAGE);
            return false;
        } else if (edtDataMatricula.getDate().toString().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Atenção! Preencha o campo Data da Matrícula", "Data da Matrícula", JOptionPane.WARNING_MESSAGE);
            return false;
        } else {
            return true;
        }
    }

    public void LimpaCampos() {
        edtCodigoMatricula.setText("");
        edtDataMatricula.setDate(new Date());
        edtDataNascimento.setDate(new Date());
        edtNome.setText("");
        edtSobrenome.setText("");
    }
}
